﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SpeechLib;//播放声音，引用:COM 的Microsoft Speech Object Library
using System.Speech.Synthesis;//播放声音，引用：程序集的 System.Speech
using System.Speech.Recognition;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using CoreShop.Model;

namespace CoreShop.App
{
    public class SoundHelper
    {
        private static dynamic _comTts;
        private static Type type = Type.GetTypeFromProgID("SAPI.SpVoice");
        private static void Read(string text)
        {
            Task.Factory.StartNew(() =>
            {
                try
                {
                    _comTts = Activator.CreateInstance(type);
                    _comTts.Rate = AppConstVars.systemsetup.speechRate;//速度
                    //_comTts.SelectVoiceByHints(VoiceGender.Female);//女：VoiceGender.Female；男VoiceGender.Male     
                    _comTts.Speak(text);
                }
                catch (Exception ex) {
                    //MessageBox.Show(ex.Message);
                }
            });

        }

        //public static SpeechSynthesizer speech = null;//windows10 报错："POS.SysClass"的类型初始值设定项引发异常        
        private static void ThreadMethodSpeech(string sStr)
        {
            try
            {
                SpeechSynthesizer speech = new SpeechSynthesizer();   //实例化对象
                speech.Rate = AppConstVars.systemsetup.speechRate;//速度
                speech.Volume = 100;//音量,0-100
                //speech.SpeakAsync(sStr);//异步播放，但是要等到前面的发音完成后才会播放该发音                
                //speech.SelectVoiceByHints(VoiceGender.Female);//女：VoiceGender.Female；男VoiceGender.Male                
                speech.Speak(sStr);
                speech.SetOutputToDefaultAudioDevice();
            }
            catch (Exception ex)
            {
                //MessageBox.Show(ex.Message);
            }
        }
        public static void SoundPlayer(string sStr)
        {
            //if (SysVar.mFSpeechGoods == 0) return;
            Thread objThread = new Thread(new ThreadStart(delegate
            {
                ThreadMethodSpeech(sStr);
            }));
            objThread.Start();
        }

        public static void SetSpeechPayInfo(string sStr)
        {
            if (AppConstVars.systemsetup.speechPayInfo == 0) return;
            sStr = sStr.Replace("支付支付", "支付");//有些支付方式本来有支付二字，替换
            if (AppConstVars.systemsetup.speechPayInfo == (int)Library.GlobalEnumVars.SpeechType.SpeechOne) 
                VoiceHelp.PlayVoiceStr(sStr); 
            else if (AppConstVars.systemsetup.speechPayInfo == (int)Library.GlobalEnumVars.SpeechType.SpeechTrow) 
                SoundPlayer(sStr);
            else if (AppConstVars.systemsetup.speechPayInfo == (int)Library.GlobalEnumVars.SpeechType.SpeechThree) 
                Read(sStr);
        }

        public static void SetAiGoodsVoice(corecmsfoodordering ordering)
        {
            if (AppConstVars.systemsetup.paraPartialModel.aiSelGoodsVoice == 0) return;
            string sStr = string.Format("{0}{1}元", ordering.name, ordering.allAmount.ToString("0.##"));
            if (AppConstVars.systemsetup.paraPartialModel.aiSelGoodsVoice == (int)Library.GlobalEnumVars.SpeechType.SpeechOne)
                VoiceHelp.PlayVoiceStr(sStr);
            else if (AppConstVars.systemsetup.paraPartialModel.aiSelGoodsVoice == (int)Library.GlobalEnumVars.SpeechType.SpeechTrow)
                SoundPlayer(sStr);
            else if (AppConstVars.systemsetup.paraPartialModel.aiSelGoodsVoice == (int)Library.GlobalEnumVars.SpeechType.SpeechThree)
                Read(sStr);
        }

    }
}
